import {MODE, BASE_NAME} from '@/store/config'
import JsCookie from 'js-cookie'

export const getKey = (name:string) => {
  return BASE_NAME + '-' + MODE + '-' + name
}

//获取本地存储
export const getLocalData = (_key:string) => {
  const key = getKey(_key)
  return localStorage.getItem(key)
}

//设置本地存储 长度5M或10M 可能储存已满
export const setLocalData = (_key:string, value:string):Promise<unknown> => {
  return new Promise((resolve, reject) => {
    const key = getKey(_key)
    try {
      localStorage.setItem(key, value)
      resolve(undefined)
    }
    catch (error) {
      reject()
    }
  })
}

//删除本地存储数据
export const removeLocalData = (_key:string) => {
  const key = getKey(_key)
  localStorage.removeItem(key)
}

//清除本地存储数据
export const clearAllLocalData = () => {
  localStorage.clear()
}

//设置用户Token
export const setCookie = (name:string, value:string) => {
  const key = getKey(name)
  JsCookie.set(key, value, {expires:7})
}

//获取用户Token
export const getCookie = (name:string):string => {
  const key = getKey(name)
  return JsCookie.get(key)
}

//清除用户Token
export const deleteCookie = (name:string) => {
  const key = getKey(name)
  JsCookie.remove(key)
}

//保存的报告打印机字段
export const reportPrinter = 'report-printer'

//保存的条码打印机字段
export const barcodePrinter = 'barcode-printer'

//保存的PDF打印机字段
export const pdfPrinter = 'pdf-printer'

//保存用户选择的报告打印机
export const saveReprotPrint = (value) => {
  return setLocalData(reportPrinter, value)
}

//保存用户选择的条码打印机
export const saveBarcodePrint = (value) => {
  return setLocalData(barcodePrinter, value)
}

//保存用户选择的PDF打印机
export const savePdfPrint = (value) => {
  return setLocalData(pdfPrinter, value)
}

//获取用户选择的报告打印机
export const getReprotPrint = () => {
  return getLocalData(reportPrinter)
}

//获取用户选择的条码打印机
export const getBarcodePrint = () => {
  return getLocalData(barcodePrinter)
}
//获取用户选择的条码打印机
export const getPdfPrint = () => {
  return getLocalData(pdfPrinter)
}

//前端登录时间
export const loginTime = 'login-time'